package com.dankegongyu.lib.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatDialog;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2020/4/28 11:36
 */
@SuppressWarnings("unchecked")
public abstract class DKBaseDialog<T extends DKBaseDialog> extends AppCompatDialog implements IDKDialog {

    private static final String TAG = DKBaseDialog.class.getSimpleName();

    private int width; // 宽度
    private int height; // 高度
    private float dimAmount = 0.5f; // 透明度
    private boolean showBottom; // 是否底部显示
    @StyleRes
    private int windowAnim;

    public DKBaseDialog(@NonNull Context context) {
        super(context);
    }

    public DKBaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DKBaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int contentViewLayoutId = getContentViewLayoutId();
        if (contentViewLayoutId != 0) {
            setContentView(contentViewLayoutId);
        }

        init();

        doBusiness();
    }

    public T width(int width) {
        this.width = width;
        return (T) this;
    }

    public T height(int height) {
        this.height = height;
        return (T) this;
    }

    public T dimAmount(@FloatRange(from = 0.0, to = 1.0) float dimAmount) {
        this.dimAmount = dimAmount;
        return (T) this;
    }

    public T showBottom(boolean showBottom) {
        this.showBottom = showBottom;
        return (T) this;
    }

    public T windowAnimations(@StyleRes int animResId) {
        this.windowAnim = animResId;
        return (T) this;
    }

    @Override
    public void show() {
        super.show();
        setWindowAttributes();
    }

    private void setWindowAttributes() {
        Window window = getWindow();
        if (window != null) {
            // 设置背景颜色,只有设置了这个属性,宽度才能全屏MATCH_PARENT
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams lp = window.getAttributes();
            // 调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = dimAmount;
            // 是否在底部显示
            if (showBottom) {
                lp.gravity = Gravity.BOTTOM;
                if (windowAnim == 0) {
                    windowAnim = R.style.DKDialogDefaultAnimation;
                }
            }

            // 设置dialog宽度
            if (width > 0) {
                lp.width = width;
            } else {
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            }

            // 设置dialog高度
            if (height > 0) {
                lp.height = height;
            } else {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            }

            // 设置dialog进入、退出的动画
            if (windowAnim != 0) {
                window.setWindowAnimations(windowAnim);
            }
            window.setAttributes(lp);
        }
    }

    public void destroy() {
        if (isShowing()) {
            dismiss();
        }
    }
}
